//商品相关方法

const {
    execSQL
} = require('../db/mysql')


//取消超时未支付的订单
const cancelExpiredOrder = ()=>{
    console.log('取消超时未支付的订单')
    
    let sql = `update orders set  canceled=${true} where expire <= '${new Date(Date.now()).toISOString().replace('T', ' ').slice(0, -5)}'`;
    execSQL(sql);
}

//获取订单详情
const getUserOrder = async ({
    uid,i
}) => {

    
    let sql = `select address_i,created,data,payed,expire,received,price,count,canceled from  orders where userId ='${uid}'  and i = '${i}'`;
    return execSQL(sql).then((res) => {
        
        return res.length ? res:false;
    }).catch(e => {
         throw e;
        return false;
    })
}

//添加订单
const updateOrder = ({uid,data,payed,count,price,address_i}) =>{
    let sql = '';
    if(payed){
         sql = `insert into orders (address_i,userId,data,payed,count,price) values('${address_i}','${uid}','${JSON.stringify(data)}','${new Date(Date.now()).toISOString().replace('T', ' ').slice(0, -5)}',${count},${price})`;
    }else{
        sql = `insert into orders (address_i,userId,data,expire,count,price) values('${address_i}','${uid}','${JSON.stringify(data)}','${new Date(Date.now() + (24*60*60*1000)).toISOString().replace('T', ' ').slice(0, -5)}',${count},${price})`;
    }
     
    return execSQL(sql).then((res) => {
        
        return res.affectedRows > 0;
    }).catch(e => {
        throw e;
        return false;
    })
}

//获取未支付订单
const getUnpayOrder = async ({
    uid
}) => {
    await cancelExpiredOrder();
    let sql = `select i,address_i,created,data,expire,price,count from  orders where userId ='${uid}' and payed IS NULL and canceled =${false}  order by created Desc`;
    return execSQL(sql).then((res) => {
        
       return res;
    }).catch(e => {
         throw e;
        return false;
    })
}

//获取未收货订单
const getUntakeOrder = async ({
    uid
}) => {
    await cancelExpiredOrder();
    let sql = `select i,address_i,created,data,price,count from  orders where userId ='${uid}' and received IS NULL and payed IS NOT NULL  and canceled=${false}  order by created Desc`;
    return execSQL(sql).then((res) => {
        
        return res;
    }).catch(e => {
         throw e;
        return false;
    })
}

//获取已完成订单
const getUnfinishedOrder = async ({
    uid
}) => {
    await cancelExpiredOrder();
    let sql = `select i,address_i,created,data,received,price,count from  orders where userId ='${uid}' and received IS NOT NULL and payed IS NOT NULL and canceled=${false}  order by created Desc`;
    return execSQL(sql).then((res) => {
        
       return res;
    }).catch(e => {
         throw e;
        return false;
    })
}

//订单已付款
const setOrderPayed = ({
    uid,
    i
}) => {
    let sql = `update orders set  payed='${new Date(Date.now()).toISOString().replace('T', ' ').slice(0, -5)}' where userId ='${uid}' and i='${i}' `;
    return execSQL(sql).then((res) => {
        
        return res.affectedRows > 0;
    }).catch(e => {
         throw e;
        return false;
    })
}



//订单已收货
const setOrderReceived = ({
    uid,
    i
}) => {

    let sql = `update orders set  received='${new Date(Date.now()).toISOString().replace('T', ' ').slice(0, -5)}' where userId ='${uid}' and i='${i}'`;
    return execSQL(sql).then((res) => {
        
        return res.affectedRows > 0;
    }).catch(e => {
         throw e;
        return false;
    })
}

//取消订单
const cancelOrder = ({
    uid,
    i
}) => {

    let sql = `update orders set  canceled=${true} , expire='${new Date(Date.now()).toISOString().replace('T', ' ').slice(0, -5)}' where userId ='${uid}' and i='${i}'`;
    return execSQL(sql).then((res) => {
        return res.affectedRows > 0;
    }).catch(e => {
         throw e;
        return false;
    })
}


//获取已取消订单
const getCanceledOrder = ({uid}) => {
    let sql = `select i,created,data,price,count from  orders where userId ='${uid}' and canceled = ${true}  order by created Desc`;
    return execSQL(sql).then((res) => {
        
       return res;
    }).catch(e => {
         throw e;
        return false;
    })
}





module.exports = {
    
    getUserOrder,
    updateOrder,
    getUnpayOrder,
    getUntakeOrder,
    getUnfinishedOrder,
    setOrderPayed,
    setOrderReceived,
    cancelOrder,
    getCanceledOrder
}